﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EnergyConsumptionMonitorSystem.MainWindowTabItem
{
    /// <summary>
    /// Interaction logic for PersonalInfo.xaml
    /// </summary>
    public partial class PersonalInfoUC : UserControl, ITabItemContentUC
    {
        private MainWindowTabItem _container;
        public MainWindowTabItem Container
        {
            get
            {
                return this._container;
            }
            set
            {
                if (this._container != value)
                {
                    if (this._container != null)
                    {
                        this._container.ClosingEvent -= this.tabItem_ClosingEvent;
                    }
                    this._container = value;
                    value.ClosingEvent += new System.ComponentModel.CancelEventHandler(tabItem_ClosingEvent);
                }
            }
        }

        public PersonalInfoUC()
        {
            InitializeComponent();
        }

        private void tabItem_ClosingEvent(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }

        public event EventHandler CloseRequestEvent;

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            if (txtAge.Text != "" && txtStature.Text != "" && txtWeight.Text != "")
            {
                bool sex;
                if (rdoMale.IsChecked == true)
                {
                    sex = true;
                }
                else
                {
                    sex = false;
                }
                int age = int.Parse(txtAge.Text);
                double stature = double.Parse(txtStature.Text);
                double weight = double.Parse(txtWeight.Text);
                double rangeMin = 18 * stature * stature / 10000.0;
                double rangeMax = 24 * stature * stature / 10000.0;
                double bmi = weight / stature / stature * 10000.0;
                double standard;
                if (sex)
                {
                    standard = (stature - 80.0) * 0.7;
                }
                else
                {
                    standard = (stature - 70.0) * 0.6;
                }
                double bmr;
                if (sex)
                {
                    bmr = 66.0 + 13.7 * weight + 5.0 * stature - 6.8 * age;
                }
                else
                {
                    bmr = 655.0 + 9.6 * weight + 1.8 * stature - 4.7 * age;
                }
                rtbResult.Document.Blocks.Clear();

                Paragraph paragraph = new Paragraph();
                AddNewBasicRun(paragraph, "您的健康体重范围为 ");
                AddNewValueRun(paragraph, Math.Round(rangeMin, 1, MidpointRounding.AwayFromZero) + " - " + Math.Round(rangeMax, 1, MidpointRounding.AwayFromZero) + " ");
                AddNewBasicRun(paragraph, "KG（1KG=2斤）\r\n\r\n");
                AddNewBasicRun(paragraph, "您的标准体重为 ");
                AddNewValueRun(paragraph, Math.Round(standard, 1, MidpointRounding.AwayFromZero).ToString());
                AddNewBasicRun(paragraph, "KG（1KG=2斤）\r\n\r\n");
                AddNewBasicRun(paragraph, "您的BMI指数为 ");
                AddNewValueRun(paragraph, Math.Round(bmi, 1, MidpointRounding.AwayFromZero).ToString() + "\r\n\r\n");
                AddNewBasicRun(paragraph, "您的基础代谢率为 ");
                AddNewValueRun(paragraph, Math.Round(bmr, 1, MidpointRounding.AwayFromZero).ToString());
                AddNewBasicRun(paragraph, "大卡\r\n\r\n");

                rtbResult.Document.Blocks.Add(paragraph);
            }
            else
            {
                MessageBox.Show("请填写完整个人资料！", "错误");
            }
        }

        private void AddNewRun(Paragraph paragraph, string text, Brush foreground)
        {
            Run run = new Run(text);
            run.FontSize = 18;
            run.FontFamily = new FontFamily("STSong");
            run.Foreground = foreground;
            run.FontWeight = FontWeights.Bold;
            paragraph.Inlines.Add(run);
        }

        private void AddNewBasicRun(Paragraph paragraph, string text)
        {
            AddNewRun(paragraph, text,Brushes.Black);
        }

        private void AddNewValueRun(Paragraph paragraph, string text)
        {
            AddNewRun(paragraph, text,Brushes.Red);
        }
    }
}
